import request from '../../utils/request'

Page({
  data: {
    form: {
      id: null,
      name: '',
      cover: '',
      prompt: '',
      negativePrompt: '',
      enabled: true,
      parameter: {
        batchCount: 1,
        batchSize: 4,
        steps: 30,
        cfgScale: 7.0,
        denoisingStrength: 0.5
      }
    },
    styleList: [], // 风格列表
    selectedStyleName: '', // 展示用
  },


  onLoad() {
    this.loadStyleList();
  },
  loadStyleList() {
    request({
      url: 'generate/prompts',
      method: 'GET'
    })
    .then(res => {
      if (res.code === 0) {
        this.setData({
          styleList: res.data
        });
        let selected = res.data[0]; // 正确获取第一个元素
        if (selected && !this.data.form.id) {
          this.setData({
            selectedStyleName: selected.name,
            form: {
              ...this.data.form,
              id: selected.id,
              name: selected.name,
              cover: selected.cover,
              prompt: selected.prompt,
              negativePrompt: selected.negativePrompt,
              enabled:selected.enabled,
              parameter: {
                ...selected.parameter
              }
            }
          });
        }
        


      } else {
        wx.showToast({
          title: '风格列表加载失败',
          icon: 'none'
        });
      }
    })
    .catch(err => {
      console.error('风格列表加载异常:', err);
      wx.showToast({
        title: '网络错误',
        icon: 'none'
      });
    });
  },
  onStyleChange(e) {
    const index = e.detail.value;
    const selected = this.data.styleList[index];

    this.setData({
      selectedStyleName: selected.name,
      form: {
        ...this.data.form,
        id: selected.id,
        name: selected.name,
        cover: selected.cover,
        prompt: selected.prompt,
        enabled: selected.enabled,
        negativePrompt: selected.negativePrompt,
        parameter: {
          ...selected.parameter
        }
      }
    });
  },
  onInputName(e) {
    this.setData({ 'form.name': e.detail.value });
  },

  onInputPrompt(e) {
    this.setData({ 'form.prompt': e.detail.value });
  },

  onInputNegativePrompt(e) {
    this.setData({ 'form.negativePrompt': e.detail.value });
  },

  onInputBatchCount(e) {
    this.setData({ 'form.parameter.batchCount': Number(e.detail.value) });
  },

  onChangingInputBatchCount(e) {
    const value = e.detail.value;
    this.setData({
      'form.parameter.batchCount': Number(e.detail.value)
    });
  },


  onInputBatchSize(e) {
    this.setData({ 'form.parameter.batchSize': Number(e.detail.value) });
  },

  onChangingInputBatchSize(e) {
    this.setData({
      'form.parameter.batchSize': Number(e.detail.value)
    });
  },


  onInputSteps(e) {
    this.setData({ 'form.parameter.steps': Number(e.detail.value) });
  },

  onChangingInputSteps(e) {
    this.setData({
      'form.parameter.steps': Number(e.detail.value)
    });
  },

  onInputCfgScale(e) {
    this.setData({ 'form.parameter.cfgScale': parseFloat(e.detail.value) });
  },

  onInputDenoisingStrength(e) {
    this.setData({ 'form.parameter.denoisingStrength': parseFloat(e.detail.value) });
  },

  chooseImage() {
    wx.chooseImage({
      count: 1,
      success: res => {
        // TODO: 你需要上传到七牛或你的服务器后返回 URL
        const tempFilePath = res.tempFilePaths[0];
        this.uploadImage(tempFilePath);
      }
    });
  },


  submitForm() {
    request({
      url: 'generate/prompts',
      method: 'PUT',
      data: this.data.form
    })
      .then(res => {
        wx.showToast({ title: '保存成功', icon: 'success' });
        // 如果需要提交成功后清空或刷新数据，可以写这里
        this.loadStyleList();
      })
      .catch(() => {
        wx.showToast({ title: '提交失败', icon: 'none' });
      });
  },



  onChangeCfgScale(e) {
    const value = e.detail.value;
    this.setData({
      'form.parameter.cfgScale': value
    });
  },
  
  onChangingCfgScale(e) {
    const value = e.detail.value;
    this.setData({
      'form.parameter.cfgScale': value
    });
  },
  
  onChangeDenoisingStrength(e) {
    const value = parseFloat(e.detail.value);
    this.setData({
      'form.parameter.denoisingStrength': value.toFixed(2)
    });
  },
  
  onChangingDenoisingStrength(e) {
    const value = parseFloat(e.detail.value);
    this.setData({
      'form.parameter.denoisingStrength': value.toFixed(2)
    });
  },

  onSwitchEnabled(e) {
    this.setData({
      'form.enabled': e.detail.value
    });
  }
  
});
